﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlowerShop.Core.Models;
using BLL.Services;

namespace FlowerShopMVC.Controllers
{
    public class OrderController : Controller
    {
        private readonly CartService _cartService = new CartService();
        private readonly OrderService _orderService = new OrderService();
        private readonly Model1 _context;


        public ActionResult ConfirmOrder()
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            var cartItems = _cartService.GetCartItems(userId);
            var totals = _cartService.CalculateCartTotals(userId);
            decimal totalPrice = totals.totalPrice;

            // 将 cartItems 和 totalPrice 作为元组返回
            var model = new Tuple<List<Tshoppingcars>, decimal>(cartItems, totalPrice);
            return View(model);
        }
        [HttpPost]
        public ActionResult SubmitOrder(List<Tshoppingcars> cartItems, decimal totalPrice, string txtAdd, string txtCity, string txtState, string txtZip, string txtPhone)
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            string userName = User.Identity.Name;
            string address = $"地址：{txtAdd}, 城市：{txtCity}, 州/省：{txtState}, 邮政编码：{txtZip}, 电话：{txtPhone}";

            // 保存订单到数据库
            _orderService.CreateOrder(userId, userName, address, cartItems, totalPrice);

            // 清空购物车
            _cartService.ClearCart(userId);

            TempData["Message"] = "订单已成功提交！";
            return RedirectToAction("Index", "Cart");
        }
        // GET: /Order/Index
        public ActionResult ApproveOrder()
        {
            // 获取所有订单
            var orders = _orderService.GetOrders(0); // 传递 0 来获取所有订单
            return View(orders);
        }


        public ActionResult Index()
        {
            // 从 Session 或其他地方获取当前登录用户的用户名
            string currentUsername = (string)Session["Username"];
            if (string.IsNullOrEmpty(currentUsername))
            {
                // 如果没有登录用户，重定向到登录页面
                return RedirectToAction("Index", "Login");
            }

            var orders = _orderService.GetOrdersByUsername(currentUsername);
            return View(orders);
        }

        [HttpPost]
        public JsonResult SignOrder(int id)
        {
            _orderService.SignOrder(id);
            return Json(new { success = true, id = id }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CommentOrder(int id)
        {
            var order = _orderService.GetOrderById(id);
            if (order == null)
            {
                return HttpNotFound("未找到订单");
            }
            return View(order);
        }
        [HttpPost]
        public ActionResult CommentOrder(int id, string comment) // 确保参数名匹配
        {
            _orderService.CommentOrder(id, comment);
            return RedirectToAction("Index");
        }


        // POST: /Order/ApproveOrder
        [HttpPost]
        public ActionResult ApproveOrder(int? id) // 改为可为空的 int
        {
            if (!id.HasValue)
            {
                // 处理 ID 为空的情况
                TempData["Error"] = "订单ID无效";
                return RedirectToAction("ApproveOrder");
            }

            _orderService.ApproveOrder(id.Value);
            TempData["Message"] = "订单已成功审核！";
            return RedirectToAction("ApproveOrder");
        }


    }
}